﻿namespace PhotobucketNet
{
    using System;
    using System.Xml;

    public class Album
    {
        private MediaItemList _mediaList;
        private string _name;
        private string _path;
        private int _photoCount;
        private PhotobucketNet.Privacy _privacy;
        private int _subAlbumCount;
        private AlbumList _subAlbums;
        private string _username;
        private int _videoCount;

        internal Album()
        {
            this._mediaList = new MediaItemList();
            this._subAlbums = new AlbumList();
        }

        public Album(string albumPath)
        {
            this._mediaList = new MediaItemList();
            this._subAlbums = new AlbumList();
            this.Path = albumPath;
        }

        internal Album(XmlNode albumNode)
        {
            this._mediaList = new MediaItemList();
            this._subAlbums = new AlbumList();
            this.ParseFromXmlNode(albumNode);
            foreach (XmlNode node in albumNode.SelectNodes("descendant::album"))
            {
                this._subAlbums.Add(CreateFromXmlNode(node));
            }
            foreach (XmlNode node2 in albumNode.SelectNodes("descendant::media"))
            {
                this._mediaList.Add(MediaItem.CreateFromXmlNode(node2));
            }
        }

        public Album(string name, string parentPath)
        {
            this._mediaList = new MediaItemList();
            this._subAlbums = new AlbumList();
            this.Path = parentPath + "/" + name;
        }

        internal static Album CreateFromXmlNode(XmlNode albumNode)
        {
            return new Album(albumNode);
        }

        public override bool Equals(object obj)
        {
            Album album = obj as Album;
            return (this.Username.Equals(album.Username) && this.Path.Equals(album.Path));
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        private void ParseFromXmlNode(XmlNode albumNode)
        {
            foreach (XmlAttribute attribute in albumNode.Attributes)
            {
                if (attribute.Name == "name")
                {
                    this.Path = attribute.Value;
                }
                else
                {
                    if (attribute.Name == "photo_count")
                    {
                        this._photoCount = Convert.ToInt32(attribute.Value);
                        continue;
                    }
                    if (attribute.Name == "subalbum_count")
                    {
                        this._subAlbumCount = Convert.ToInt32(attribute.Value);
                        continue;
                    }
                    if (attribute.Name == "video_count")
                    {
                        this._videoCount = Convert.ToInt32(attribute.Value);
                        continue;
                    }
                    if (attribute.Name == "username")
                    {
                        this._username = attribute.Value;
                    }
                }
            }
        }

        public MediaItemList MediaList
        {
            get
            {
                return this._mediaList;
            }
        }

        public string Name
        {
            get
            {
                return this._name;
            }
            set
            {
                this._path.Replace(this._name, value);
                this._name = value;
            }
        }

        public string Path
        {
            get
            {
                return this._path;
            }
            set
            {
                this._path = value;
                int num = value.LastIndexOf('/');
                if (num == -1)
                {
                    this._name = value;
                }
                else
                {
                    this._name = value.Substring(num + 1);
                }
            }
        }

        public int PhotoCount
        {
            get
            {
                return this._photoCount;
            }
        }

        public PhotobucketNet.Privacy Privacy
        {
            get
            {
                return this._privacy;
            }
            set
            {
                this._privacy = value;
            }
        }

        public int SubAlbumCount
        {
            get
            {
                return this._subAlbumCount;
            }
        }

        public AlbumList SubAlbums
        {
            get
            {
                return this._subAlbums;
            }
        }

        public string Username
        {
            get
            {
                return this._username;
            }
        }

        public int VideoCount
        {
            get
            {
                return this._videoCount;
            }
        }
    }
}

